import requests
import json


def query_rag_api(question: str):
    """
    调用RAG API的/rag/query路径

    Args:
        question (str): 要查询的问题
    """
    try:
        # API端点URL
        url = 'http://localhost:8000/rag/query'

        # 请求数据，符合QueryRequest模型要求
        payload = {
            "question": question
        }

        # 设置请求头
        headers = {
            'Content-Type': 'application/json'
        }

        # 发送POST请求
        response = requests.post(
            url,
            data=json.dumps(payload),
            headers=headers
        )

        # 检查响应状态
        response.raise_for_status()

        # 获取并打印结果
        result = response.json()
        print("API返回结果:")
        print(json.dumps(result, indent=2, ensure_ascii=False))

        return result

    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")
    except json.JSONDecodeError as e:
        print(f"JSON解析失败: {e}")
    except Exception as e:
        print(f"发生错误: {e}")


if __name__ == "__main__":
    # 示例调用
    query_rag_api("您的问题内容")
